#include "../common.h"
using namespace std;
extern BuiltinCmd blt[MAX_BUILTIN_FUNC_NUM];

int git_diff() {
    BuiltinCmd x;
    strcpy(x.name, "diff");
    strcpy(x.desc, "git diff [first-branch]...[second-branch]");
    x.hasAction = true;
    x.optNum = 2;
    strcpy(x.Help,"git diff [file]");
	strcpy(x.action, "git diff [file1] and [file2]");
	addBuiltinCmd(&x);
	
//	strcpy(x.action[1],"Reinitialized existing Git repository in");
    return 0;
}
int git_diff_cmdpline(int pos,int retc,char **retv){
	if (retc==5){
       printf("diff %s and %s\n",retv[3],retv[4]);
    }
    else if (retc==3){
    	return 0;
	}
}
int git_diff_run(int retc,char **retv){
    if (retc==5){
    	//char buf[80];
       // getcwd(buf, sizeof(buf));
        //printf("current working directory : %s\n", buf);
        //chdir("D:\\self-git\\git");
    	char file1[MAX_BUILTIN_DESC_LENGTH],file2[MAX_BUILTIN_DESC_LENGTH];
    	strcpy(file1,retv[3]);strcpy(file2,retv[4]);
    	FILE *fp1=fopen(file1,"r");
    	if (!fp1){printf("Can't find %s\n",file1);return 0;}
    	FILE *fp2=fopen(file2,"r");
    	if (!fp2){printf("Can't find %s\n",file2);return 0;}
    	char ch1[MAX_BUILTIN_DESC_LENGTH],ch2[MAX_BUILTIN_DESC_LENGTH];
    	int line=0;
		char *str1=fgets(ch1,MAX_BUILTIN_DESC_LENGTH,fp1);
		char *str2=fgets(ch2,MAX_BUILTIN_DESC_LENGTH,fp2);
		while (str1||str2){
    		++line;
    		if (strcmp(ch1,ch2)){
			  printf("line %d:\n",line);
			  if (str1)printf("%s:%s",file1,ch1);
			  if (str2)printf("%s:%s",file2,ch2);
			  printf("\n");
			}
			str1=fgets(ch1,MAX_BUILTIN_DESC_LENGTH,fp1);
			str2=fgets(ch2,MAX_BUILTIN_DESC_LENGTH,fp2);
		}
		fclose(fp1);fclose(fp2);
	}
	else if (retc==3){
		return 0;
	}
}
